home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12950 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  38 lines

  1. Newsgroups: comp.lang.c,comp.lang.perl
  2. Path: news.inap.net!news1!ind-004-236-188
  3. From: dlmiller@iquest.net (Doug Miller)
  4. Subject: Re: Random File Access - I don't get it
  5. X-Nntp-Posting-Host: ind-004-236-188.iquest.net
  6. Message-ID: <DpAIH0.2yC@iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Network Services
  9. X-Newsreader: News Xpress Version 1.0 Beta #2.1
  10. References: <3160DE1E.495C@teleport.com>
  11. Date: Wed, 3 Apr 1996 14:22:08 GMT
  12.  
  13. Scott Kinard <kinards@teleport.com> wrote:
  14. >Greetings,
  15. >
  16. >  As I was coding a simple program to randomly read lines from a text file it occurred
  17. >to me something was amiss. This is probably some fundamental oversight on my part, but
  18. >some illumination would be helpful...
  19. >
  20. >  Suppose I have two files, one contains text (1 line of random length text per
  21. >'record') and another which is my index into the text file, which contains the starting
  22. >and ending byte positions in the text file for each 'record'. Now the question..
  23. >
  24. >Suppose the text file has 2,000,000 entries. Now, what's the difference in reading
  25. >1,000,000 lines from the index file to find the starting and ending byte positions in
  26. >the text file for record 1,000,000, and then seeking these in the text file, rather than
  27. >just reading 1,000,000 times from the text file to get the same 'record' in the first
  28. >place?
  29. >
  30. >-Scott
  31.  
  32. The *index* file would have fixed-length records.  Say for example that the start and end positions
  33. are each 2-byte ints; then each record in the index file would be 4 bytes long.  The 1,000,000th
  34. record in the index file would thus be located at an offset of (1M - 1) * 4 = 3,999,996 bytes. lseek
  35. to that offset in the index file and read *one* index record to retrieve the starting and ending
  36. addresses in the data file.  Compute the length, lseek to the starting address, and read *one*
  37. record for the computed length.  Two reads.
  38.